//
//  LGPhotoEditerViewController.m
//  wooplus
//
//  Created by 李刚 on 2018/11/24.
//  Copyright © 2018 李刚. All rights reserved.
//

#import "LGPhotoEditerViewController.h"

#define CLIP_WIDTH (300)
#define CLIP_HEIGHT (300)

@interface LGOverlay : UIView

@property (nonatomic) CALayer *maskLayer;

@end

@implementation LGOverlay

- (void)drawRect:(CGRect)rect {
    
    [[[UIColor whiteColor] colorWithAlphaComponent:1] set];
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake((rect.size.width-CLIP_WIDTH)/2, (rect.size.height-CLIP_HEIGHT)/2, 300, 300)];
    [path fill];
    [path stroke];
}

@end

@interface LGPhotoEditerViewController ()<UIScrollViewDelegate>

@property(nonatomic) UIView *contentView;
@property(nonatomic) UIScrollView *scrollView;
@property(nonatomic) UIImageView *selectedImageView;
@property(nonatomic) LGOverlay *overlay;

@property(nonatomic) UIButton *cancelBtn;
@property(nonatomic) UIButton *doneBtn;

@end

@implementation LGPhotoEditerViewController

- (void)loadView {
    [super loadView];
    [self.view setBackgroundColor:[UIColor blackColor]];
    
    self.contentView = [[UIView alloc] init];
    [self.view addSubview:self.contentView];
    
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.bouncesZoom = YES;
    self.scrollView.alwaysBounceVertical = YES;
    self.scrollView.alwaysBounceHorizontal = YES;
    self.scrollView.minimumZoomScale = 1;
    self.scrollView.maximumZoomScale = 3;
    self.scrollView.zoomScale = 1;
    self.scrollView.delegate = self;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    [self.scrollView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
    [self.contentView addSubview:self.scrollView];
    
    self.selectedImageView = [[UIImageView alloc] init];
    self.selectedImageView.userInteractionEnabled = YES;
    [self.scrollView addSubview:self.selectedImageView];
    
    self.overlay = [[LGOverlay alloc] init];
    [self.overlay setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.5]];
    [self.contentView setMaskView:self.overlay];
    
    self.cancelBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.cancelBtn setTitle:@"cancel" forState:UIControlStateNormal];
    [self.cancelBtn setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:self.cancelBtn];
    
    self.doneBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.doneBtn setTitle:@"done" forState:UIControlStateNormal];
    [self.doneBtn setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:self.doneBtn];
}

- (void)viewWillLayoutSubviews {
    CGFloat width = self.view.bounds.size.width;
    CGFloat height = self.view.bounds.size.height;
    
    CGFloat left = ABS((width - CLIP_WIDTH)/2);
    CGFloat top = ABS((height - CLIP_HEIGHT)/2);
    
    [self.contentView setFrame:self.view.bounds];
    [self.scrollView setFrame:self.view.bounds];
    [self.scrollView setContentInset:UIEdgeInsetsMake(top,left,top,left)];
    [self.overlay setFrame:self.view.bounds];
    
    [self.cancelBtn setFrame:CGRectMake(0, height-50, width/2, 50)];
    [self.doneBtn setFrame:CGRectMake(width/2, height-50, width/2, 50)];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupInterface];
    
    [self.cancelBtn addTarget:self action:@selector(handleCancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.doneBtn addTarget:self action:@selector(handleDoneBtnClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)setupInterface {
    
    [self setupImageView];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}
#pragma mark - get/set
- (void)setOriginImage:(UIImage *)originImage {
    _originImage = originImage;
    [self setupImageView];
}
#pragma mark - private
- (void)setupImageView {
    CGFloat img_width=0;
    CGFloat img_height=0;
    
    if (self.originImage.size.width<self.originImage.size.height) {
        img_width = CLIP_WIDTH;
        img_height = self.originImage.size.height * (img_width/self.originImage.size.width);
    } else {
        img_height = CLIP_HEIGHT;
        img_width = self.originImage.size.width * (img_height/self.originImage.size.height);
    }
    
    [self.selectedImageView setImage:self.originImage];
    self.selectedImageView.frame = CGRectMake(0,0, img_width, img_height);
    self.scrollView.contentSize = CGSizeMake(img_width, img_height);
}

- (UIImage *)cropImage {

    CGFloat left = ABS(self.scrollView.bounds.size.width-CLIP_WIDTH)/2;
    CGFloat top = ABS(self.scrollView.bounds.size.height-CLIP_HEIGHT)/2;
    
    CGRect clipRect = CGRectMake(left, top, CLIP_WIDTH, CLIP_HEIGHT);
    CGRect rect = [self.view convertRect:clipRect toView:self.selectedImageView];

    CGFloat zoom = self.selectedImageView.bounds.size.width/(self.originImage.size.width*self.originImage.scale);
    
    CGRect rec = CGRectMake(rect.origin.x/zoom, rect.origin.y/zoom,rect.size.width/zoom,rect.size.height/zoom);
    CGImageRef imageRef =CGImageCreateWithImageInRect([self.originImage CGImage],rec);
    UIImage * image = [[UIImage alloc] initWithCGImage:imageRef];
    CGImageRelease(imageRef);
    
    return image;
}
#pragma mark - handle
- (IBAction)handleCancelBtnClick:(UIButton *)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(didCancelPhotoEditer:)]) {
        [self.delegate didCancelPhotoEditer:self];
    }
}
- (IBAction)handleDoneBtnClick:(UIButton *)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(photoEditer:didDoneWithImage:)]) {
        [self.delegate photoEditer:self didDoneWithImage:[self cropImage]];
    }
}
#pragma mark - delegate
#pragma mark <UIScrollViewDelegate>
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return self.selectedImageView;
}

@end
